<?php

/**
 * nodo2 actions.
 *
 * @package    sensores
 * @subpackage nodo2
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class nodo2Actions extends sfActions
{
    /*****************************************************************/
  public function executeMenu_graficos(sfWebRequest $request){}
  public function executeBarra(sfWebRequest $request){}
  public function executeDatosbarra(sfWebRequest $request)
  {        
	$categorias = array('nombre');
	$sensor1 = array('Temperatura');
	$sensor2 = array('Humedad');
        $sensor3 = array('Luz');
        $sensor4 = array('Voltaje');
        
        $query = "SELECT nombre FROM red_sensores WHERE (nombre = 'mota_2');"; 
        $con = Doctrine_Manager::getInstance()->connection();
        $st = $con->execute($query);
        $resultado = $st->fetchAll();
                
        foreach($resultado as $item):
            $categorias[] = $item[0];
        endforeach;

        $query = "SELECT nombre, temperatura, humedad, luz, voltaje  FROM red_sensores  WHERE (nombre='mota_2');"; 
        $con = Doctrine_Manager::getInstance()->connection();
        $st = $con->execute($query);
        $resultado = $st->fetchAll();
        
        foreach($resultado as $elemento):            
            if($elemento[0] == 'mota_2'):
                $sensor1[] = (double) $elemento[1];
                $sensor2[] = (double) $elemento[2];
                $sensor3[] = (double) $elemento[3];
                $sensor4[] = (double) $elemento[4];            
            endif;
        endforeach;
        
        echo json_encode( array($categorias, $sensor1, $sensor2, $sensor3, $sensor4) );

  }
  public function executeDashboard(sfWebRequest $request){}
  public function executeDatosdashboard(sfWebRequest $request)
  {
        
      $categorias = array('nombre');
	$valor1 = array('nombre');
	$valor2 = array('temperatura');        
        $valor3 = array('humedad');
        $valor4 = array('luz');
        $valor5 = array('cantidad');
        
        $query = "SELECT nombre FROM nodo1 WHERE (nombre = 'mota_1') OR (nombre = 'mota_2') GROUP BY(nombre);"; 
        $con = Doctrine_Manager::getInstance()->connection();
        $st = $con->execute($query);
        $resultado = $st->fetchAll();
                
        foreach($resultado as $item):
            $categorias[] = $item[0];
        endforeach;

        $query1 = "SELECT nombre, AVG(temperatura) as temperatura, AVG(humedad)as humedad, AVG(luz) as luz, count(*) as cantidad_registros FROM nodo1 WHERE (MINUTE(hora) >= 1) AND (MINUTE(hora) <= 10) GROUP BY nombre;";
        $con = Doctrine_Manager::getInstance()->connection();
        $st = $con->execute($query1);
        $resultado = $st->fetchAll();
        
        foreach($resultado as $elemento):            
            if($elemento[0] == 'mota_1'):
                
                $valor2[] = (int) $elemento[1];
                $valor3[] = (int) $elemento[2];
               $valor4[] = (int) $elemento[3];
//                $valor5[] = (int) $elemento[4];
            
            elseif($elemento[0] == 'mota_2'):
                
                $valor2[] = (int) $elemento[1];
                $valor3[] = (int) $elemento[2];
                $valor4[] = (int) $elemento[3];
//                $valor5[] = (int) $elemento[4];
                
             endif;            
        endforeach;
        
        echo json_encode( array($categorias, $valor2, $valor3, $valor4/*, $valor5*/) );                    
  }
  public function executeArea(sfWebRequest $request){}
  public function executeDatosarea(sfWebRequest $request)
  {
      $categorias = array();
	$valor1 = array(10);
        $valor2 = array(20);
        $valor3 = array('30');        
        
        
//        $query = "SELECT nombre FROM nodo1 WHERE (nombre = 'mota_1') GROUP BY(nombre);"; 
//        $con = Doctrine_Manager::getInstance()->connection();
//        $st = $con->execute($query);
//        $resultado = $st->fetchAll();
//                
//        foreach($resultado as $item):
//            $categorias[] = $item[0];
//        endforeach;
            $categorias[] = 'temperatura1';
            $categorias[] = 'temperatura2';
            $categorias[] = 'temperatura3';
            $categorias[] = 'temperatura4';
            $categorias[] = 'temperatura5';            
            $categorias[] = 'temperatura6';
        //$query1 = "SELECT nombre, AVG(temperatura) as temperatura, AVG(humedad)as humedad, AVG(luz) as luz, count(*) as cantidad_registros FROM nodo1 WHERE (MINUTE(hora) >= 1) AND (MINUTE(hora) <= 10) GROUP BY nombre;";
        $query1 = "SELECT nombre, AVG(temperatura) AS temperatura FROM nodo1 WHERE ((MINUTE(hora) >= 1) AND (MINUTE(hora) <= 10)) GROUP BY(nombre) UNION ";
        $query1 .= "SELECT nombre, AVG(temperatura) AS temperatura FROM nodo1 WHERE ((MINUTE(hora) >= 10) AND (MINUTE(hora) <= 20)) GROUP BY(nombre) UNION ";
        $query1 .="SELECT nombre, AVG(temperatura) AS temperatura FROM nodo1 WHERE ((MINUTE(hora) >= 20) AND (MINUTE(hora) <= 30)) GROUP BY(nombre) UNION ";
        $query1 .="SELECT nombre, AVG(temperatura) AS temperatura FROM nodo1 WHERE ((MINUTE(hora) >= 30) AND (MINUTE(hora) <= 40)) GROUP BY(nombre) UNION ";
        $query1 .= "SELECT nombre, AVG(temperatura) AS temperatura FROM nodo1 WHERE ((MINUTE(hora) >= 40) AND (MINUTE(hora) <= 50)) GROUP BY(nombre) UNION ";
        $query1 .= "SELECT nombre,  AVG(temperatura) AS temperatura FROM nodo2 WHERE ((MINUTE(hora) >= 1) AND (MINUTE(hora) <= 10)) GROUP BY(nombre) UNION ";
        $query1 .= "SELECT nombre,  AVG(temperatura) AS temperatura FROM nodo2 WHERE ((MINUTE(hora) >= 10) AND (MINUTE(hora) <= 20)) GROUP BY(nombre) UNION ";
        $query1 .="SELECT nombre,  AVG(temperatura) AS temperatura FROM nodo2 WHERE ((MINUTE(hora) >= 20) AND (MINUTE(hora) <= 30)) GROUP BY(nombre) UNION ";
        $query1 .="SELECT nombre,  AVG(temperatura) AS temperatura FROM nodo2 WHERE ((MINUTE(hora) >= 30) AND (MINUTE(hora) <= 40)) GROUP BY(nombre) UNION ";
        $query1 .="SELECT nombre, AVG(temperatura) AS temperatura FROM nodo2 WHERE ((MINUTE(hora) >= 40) AND (MINUTE(hora) <= 50)) GROUP BY(nombre);";

        
               
        $con = Doctrine_Manager::getInstance()->connection();
        $st = $con->execute($query1);
        $resultado = $st->fetchAll();
        //print_r($resultado);
        foreach($resultado as $elemento):
            //print_r($elemento);
            if ($elemento[0] == 'mota_1'):
                $valor1[] = (int) $elemento[1];
                
            elseif ($elemento[0] == 'mota_2'):
                $valor2[] = (int) $elemento[1];
                
                endif;
        endforeach;        
        
        echo json_encode( array($categorias, $valor1, $valor2) );                    
  }
 /*****************************************************************/
  public function executeIndex(sfWebRequest $request)
  {
    $this->nodo2s = Doctrine_Core::getTable('Nodo2')
      ->createQuery('a')
      ->execute();
  }

  public function executeShow(sfWebRequest $request)
  {
    $this->nodo2 = Doctrine_Core::getTable('Nodo2')->find(array($request->getParameter('id')));
    $this->forward404Unless($this->nodo2);
  }

  public function executeNew(sfWebRequest $request)
  {
    $this->form = new Nodo2Form();
  }

  public function executeCreate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST));

    $this->form = new Nodo2Form();

    $this->processForm($request, $this->form);

    $this->setTemplate('new');
  }

  public function executeEdit(sfWebRequest $request)
  {
    $this->forward404Unless($nodo2 = Doctrine_Core::getTable('Nodo2')->find(array($request->getParameter('id'))), sprintf('Object nodo2 does not exist (%s).', $request->getParameter('id')));
    $this->form = new Nodo2Form($nodo2);
  }

  public function executeUpdate(sfWebRequest $request)
  {
    $this->forward404Unless($request->isMethod(sfRequest::POST) || $request->isMethod(sfRequest::PUT));
    $this->forward404Unless($nodo2 = Doctrine_Core::getTable('Nodo2')->find(array($request->getParameter('id'))), sprintf('Object nodo2 does not exist (%s).', $request->getParameter('id')));
    $this->form = new Nodo2Form($nodo2);

    $this->processForm($request, $this->form);

    $this->setTemplate('edit');
  }

  public function executeDelete(sfWebRequest $request)
  {
    $request->checkCSRFProtection();

    $this->forward404Unless($nodo2 = Doctrine_Core::getTable('Nodo2')->find(array($request->getParameter('id'))), sprintf('Object nodo2 does not exist (%s).', $request->getParameter('id')));
    $nodo2->delete();

    $this->redirect('nodo2/index');
  }

  protected function processForm(sfWebRequest $request, sfForm $form)
  {
    $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));
    if ($form->isValid())
    {
      $nodo2 = $form->save();

      $this->redirect('nodo2/edit?id='.$nodo2->getId());
    }
  }
}
